package com.mimi.practice.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.transition.ChangeBounds;
import androidx.transition.TransitionManager;
import androidx.transition.TransitionSet;

import com.google.android.material.transition.MaterialFade;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.mimi.practice.R;

public class LoadingView extends CenterPopupView {

    private TextView tv_title;
    private View progressBar, spinnerView;

    public LoadingView(@NonNull Context context, String title) {
        super(context);
        setTitle(title);
        addInnerContent();
    }

    @Override
    protected int getImplLayoutId() {
        return bindLayoutId != 0 ? bindLayoutId : R.layout.view_loading;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        tv_title = findViewById(R.id.tv_title);
        progressBar = findViewById(R.id.loadProgress);
//        spinnerView = findViewById(R.id.loadview);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getPopupImplView().setElevation(10f);
        }
        if (bindLayoutId == 0) {
            getPopupImplView().setBackground(XPopupUtils.createDrawable(Color.parseColor("#212121"), popupInfo.borderRadius));
        }
        setup();
    }

    private boolean firstShow = true;

    @Override
    protected void onShow() {
        super.onShow();
        firstShow = false;
    }

    protected void setup() {
        post(new Runnable() {
            @Override
            public void run() {
                if (!firstShow) {
                    TransitionSet set = new TransitionSet()
                            .setDuration(getAnimationDuration())
                            .addTransition(new MaterialFade())
                            .addTransition(new ChangeBounds());
                    TransitionManager.beginDelayedTransition(centerPopupContainer, set);
                }
                if (title == null || title.length() == 0) {
                    XPopupUtils.setVisible(tv_title, false);
                } else {
                    XPopupUtils.setVisible(tv_title, true);
                    if (tv_title != null) tv_title.setText(title);
                }

//                XPopupUtils.setVisible(progressBar, false);
//                XPopupUtils.setVisible(spinnerView, true);
            }
        });
    }

    private CharSequence title;

    public LoadingView setTitle(CharSequence title) {
        this.title = title;
        setup();
        return this;
    }

    public String getTitle() {
        return title.toString();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        firstShow = true;
    }
}
